package com.winapi.project.config;

import com.alipay.api.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;

/**
 * @author yyzhang
 * @version 1.0
 */
@Configuration
@PropertySource("classpath:application-alipay.properties")
public class AlipayClientConfig {
    //自动获取application-alipay.properties中的配置
    @Resource
    private Environment config;
    //创建 远程调用支付宝sdk的客户端连接对象
    // 配置好之后，调用相关接口自动对远程请求进行加签和对请求的响应进行验签
    //sign必填参数会在对象调用接口的时候自动生成，不需要设置
    @Bean
    public AlipayClient alipayClient() throws AlipayApiException{
        AlipayConfig alipayConfig = new AlipayConfig();

//设置网关地址

        alipayConfig.setServerUrl(config.getProperty("alipay.gateway-url"));

//设置应用APPID

        alipayConfig.setAppId(config.getProperty("alipay.app-id"));

//设置应用私钥

        alipayConfig.setPrivateKey(config.getProperty("alipay.merchant-private-key"));

//设置请求格式，固定值json

        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);

//设置字符集

        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);

//设置支付宝公钥

        alipayConfig.setAlipayPublicKey(config.getProperty("alipay.alipay-public-key"));

//设置签名类型

        alipayConfig.setSignType(config.getProperty("alipay.sign-type"));

//构造client

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        return alipayClient;
    }
}
